''' @author:gxj 2018/11 '''
from django.shortcuts import render,redirect,HttpResponse
from django.views import View
import datetime,logging
from app02.sql import *
import json
from app02.pager import myPaginator



def index03(request):
    '''
    分页函数 当前页，url 总数为必传、每页显示数据数量、每页显示的页码数为非必传
    '''
    current_page = request.GET.get('p')
    url='/app02/book1.html'
    totalcount=80
    page_obj = myPaginator(url,totalcount,current_page,10,7)
    data = bookes[page_obj.start():page_obj.end()]
    return render(request,'book1.html',{'data':data,"page_obj":page_obj})


# L={"current_page":current_page,"pagesize":pagesize,'pageNum':pageNum}

def student0(request):
    if request.method=='GET':
        sql='select a.id ,a.sno,a.name,a.age,a.sex,b.classname,b.classId from  db_ems.student a ,db_ems.class b WHERE a.cId=b.classId  LIMIT 10'
        sql1 = "select * from db_ems.class LIMIT 10"
        classList = sqlread(sql1)
        studentList= sqlread(sql)
        studentList = studentList[page_obj.start():page_obj.end()]
        return render(request,'student.html',locals())
    else:

        countsql = 'select count(*) from db_ems.student;'
        totalcount = sqlread(countsql)
        # sql1 = "select * from db_ems.class "
        # classList = sqlread(sql1)
        #
        # sql='select a.id ,a.sno,a.name,a.age,a.sex,b.classname,b.classId from  db_ems.student a ,db_ems.class b WHERE a.cId=b.classId  LIMIT 10'
        # studentList= sqlread(sql)
        current_page = request.POST.get("current_page")
        url='/app02/student.html'
        pageZize=request.POST.get("pagesize")
        pageNum=7
        page_obj = myPaginator(url,totalcount,current_page,pageZize,pageNum)

        return render(request,'student.html',locals())

def student(request):
        sql='select a.id ,a.sno,a.name,a.age,a.sex,b.classname,b.classId from  db_ems.student a ,db_ems.class b WHERE a.cId=b.classId'
        sql1 = "select * from db_ems.class LIMIT 10"
        classList = sqlread(sql1)
        studentList= sqlread(sql)
        return render(request,'student.html',locals())

def addStudent(request):
    response = {'status':'Ture' ,'message':None ,'data':None}
    postdata = json.loads(request.body.decode())

    try:
        name= postdata.get('username')
        age = postdata.get('age')
        sno = postdata.get('sno')
        sex = postdata.get('sex')
        cId = postdata.get('classId')
        sql="insert into student(name, age, sex, sno,cId) values('%s','%s','%s','%s','%s')"%(name, age, sex, sno,cId)
        print(sql)
        sqlwrite(sql)
    except Exception as e:
        response["status"] = False
        response["message"] ='用户输入类型错误'
    return HttpResponse(json.dumps(response))

def updateStudent(request):
    data = {"code":200,"message":"success"}
    print('request.body--->',request.body)
    postdata = json.loads(request.body.decode())
    print(type(postdata))
    try:
        id = postdata.get('id')
        name= postdata.get('username')
        age = postdata.get('age')
        sno = postdata.get('sno')
        sex = postdata.get('sex')
        cId = postdata.get('classId')
        print('cId------->',cId)
        sql="update student set  name='%s',age='%s', sex='%s', sno='%s',cId='%s' WHERE id='%s' " %(name, age, sex, sno,cId,id)
        print(sql)
        sqlwrite(sql)
    except Exception as e:
        data["code"] = 400
        data["message"] ='更新失败'
    return HttpResponse(json.dumps(data))



def delStudent(request):
    print(request)
    response = {'status':'Ture' ,'message':'删除成功'}
    data = json.loads(request.body.decode())
    print(data)
    try:
        id = data.get('nid')
        sql="DELETE FROM student WHERE id='%s' " %id
        print(sql)
        sqlwrite(sql)
    except Exception as e:
        response["status"] = False
        response["message"] ='删除失败'
    return HttpResponse(json.dumps(response))

